home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / lpc10 / vparms.c < prev   
C/C++ Source or Header  |  2000-05-18  |  4KB  |  138 lines

  1. /**********************************************************************
  2. *
  3. *    VPARMS Version 50
  4. *
  5. **********************************************************************
  6. *
  7. *  Calculate voicing parameters:
  8. *
  9. * Inputs:
  10. *  VWIN   - Voicing window limits
  11. *  INBUF  - Input speech buffer
  12. *  LPBUF  - Low pass filtered speech
  13. *  BUFLIM - Array bounds for INBUF and LPBUF
  14. *  HALF   - Half frame (1 or 2)
  15. *  DITHER - Zero crossing threshold
  16. *  MINTAU - Lag corresponding to minimum AMDF value (pitch estimate)
  17. * Outputs:
  18. *  ZC      - Zero crossing rate
  19. *  LBE      - Low band energy (sum of magnitudes - SM)
  20. *  FBE      - Full band energy (SM)
  21. *  QS      - Ratio of 6 dB/oct preemphasized energy to full band energy
  22. *  RC1      - First reflection coefficient
  23. *  AR_B   - Product of the causal forward and reverse pitch
  24. *        prediction gains
  25. *  AR_F   - Product of the noncausal forward and reverse pitch
  26. *        prediction gains
  27. * Internal:
  28. *  OLDSGN - Previous sign of dithered signal
  29. *  VLEN   - Length of voicing window
  30. *  START  - Lower address of current half of voicing window
  31. *  STOP   - Upper address of current half of voicing window
  32. *  E_0      - Energy of LPF speech (sum of squares - SS)
  33. *  E_B      - Energy of LPF speech backward one pitch period (SS)
  34. *  E_F      - Energy of LPF speech forward one pitch period (SS)
  35. *  R_B      - Autocovariance of LPF speech backward one pitch period
  36. *  R_F      - Autocovariance of LPF speech forward one pitch period
  37. *  LP_RMS - Energy of LPF speech (sum of magnitudes - SM)
  38. *  AP_RMS - Energy of all-pass speech (SM)
  39. *  E_PRE  - Energy of 6dB preemphasized speech (SM)
  40. *  E0AP   - Energy of all-pass speech (SS)
  41. */
  42. #include "lpcdefs.h"
  43. #include <math.h>
  44.  
  45. vparms(vwin, inbuf, lpbuf, half, dither, mintau, zc, lbe, fbe, qs, rc1, ar_b, ar_f )
  46. int *vwin;
  47. float *inbuf;
  48. int half, *zc, *lbe, *fbe, mintau;
  49. float *dither, *qs, *rc1, *ar_b;
  50. float *ar_f;
  51. float *lpbuf;
  52. {
  53. int i, vlen, start, stop;
  54. float oldsgn, e_0, e_b, r_b, lp_rms, ap_rms, e_pre, e0ap;
  55. float e_f, r_f;
  56. float sign;
  57. float *ptr1, *ptr2;
  58.  
  59.             /******** VWIN(1) => vwin[0][2] ********
  60.              ******** VWIN(2) => vwin[1][2] ********/
  61.  
  62. /*   Calculate zero crossings (ZC) and several energy and correlation
  63. *   measures on low band and full band speech.    Each measure is taken
  64. *   over either the first or the second half of the voicing window,
  65. *   depending on the variable HALF.    */
  66.  
  67. lp_rms = 0.;
  68. ap_rms = 0.;
  69. e_pre = 0.;
  70. e0ap = 0.;
  71. *rc1 = 0.;
  72. e_0 = 0.;
  73. e_b = 0.;
  74. e_f = 0.;
  75. r_f = 0.;
  76. r_b = 0.;
  77. *zc = 0;
  78.  
  79. vlen = *(vwin+AF+2) - *(vwin+2) + 1;
  80. start = *(vwin+2) + (half-1)*(vlen>>1) + 1;
  81. stop = start + (vlen>>1) - 1;
  82. oldsgn = (*(inbuf+start-1)-*dither < 0)?-1.:1.;
  83. ptr1 = lpbuf+start;
  84. ptr2 = inbuf+start;
  85. for(i=start; i<= stop; i++)    {
  86.     lp_rms += (float)fabs((double)(*ptr1));
  87.     ap_rms += (float)fabs((double)*ptr2);
  88.     e_pre += (float)fabs((double)(*ptr2-*(ptr2-1)));
  89.     e0ap += *ptr2**ptr2;
  90.     *rc1 += *ptr2**(ptr2-1);
  91.     e_0 += *ptr1**ptr1;
  92.     e_b += *(ptr1-mintau)**(ptr1-mintau);
  93.     e_f += *(ptr1+mintau)**(ptr1+mintau);
  94.     r_f += *ptr1**(ptr1+mintau);
  95.     r_b += *ptr1**(ptr1-mintau);
  96.     sign = (*ptr2+ *dither < 0)?-1.:1.;
  97.     if( sign != oldsgn ) {
  98.         *zc += 1;
  99.         oldsgn = -oldsgn;
  100.     }
  101.     *dither = -*dither;
  102.     ptr1++; ptr2++;
  103. }
  104.  
  105. /*   Normalized short-term autocovariance coefficient at unit sample delay    */
  106.  
  107. *rc1 /= mmax(e0ap,1.);
  108.  
  109. /*   Ratio of the energy of the first difference signal (6 dB/oct preemphasis)
  110. *   to the energy of the full band signal    */
  111.  
  112. *qs = e_pre / mmax(2.*ap_rms,1.);
  113.  
  114. /*   aR_b is the product of the forward and reverse prediction gains,
  115. *   looking backward in time (the causal case). */
  116.  
  117. /***** *ar_b = (r_b / mmax(e_b,1.)) * (r_b / mmax(e_0,1.)); *****/
  118. *ar_b = (r_b * r_b) / (mmax(e_b,1.) * mmax(e_0,1.));
  119.  
  120. /*   aR_f is the same as aR_b, but looking forward in time (non causal case).  */
  121.  
  122. *ar_f = (r_f / mmax(e_f,1.)) * (r_f / mmax(e_0,1.));
  123.  
  124. /*   Normalize ZC, LBE, and FBE to old fixed window length of 180.
  125. *   (The fraction 90/VLEN has a range of .58 to 1)        */
  126.  
  127. /*****
  128. *zc =         L_nint( *zc*2     * (90./vlen) );
  129. *lbe = mmin( L_nint( lp_rms*0.25 * (90./vlen) ), 32767 );
  130. *fbe = mmin( L_nint( ap_rms*0.25 * (90./vlen) ), 32767 );
  131. *****/
  132. *zc = ( *zc*2      * (90./vlen) ) + .5;
  133. *lbe = mmin( ( lp_rms*0.25 * (90./vlen) )+.5, 32767 );
  134. *fbe = mmin( ( ap_rms*0.25 * (90./vlen) )+.5, 32767 );
  135.  
  136.  
  137. }
  138.